package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.UserFollowList;
import com.hhs.storage.dao.UserFollowListDao;
import com.hhs.storage.service.UserFollowListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.SysUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-03-10
 */
@Service
@Transactional
public class UserFollowListServiceImpl extends ServiceImpl<UserFollowListDao, UserFollowList> implements UserFollowListService {
    @Resource
    private HttpServletRequest request;
    @Override
    public Map<String, Object> add(String followUserId,String groupId) {
        QueryWrapper<UserFollowList> qw=new QueryWrapper<>();
        qw.eq("follow_user_id",followUserId);
        qw.eq("user_id",SysUtil.getLoginUserId(request));
        qw.eq("group_id",groupId);
        if (this.count(qw)>0){
            return ResponseInfo.error("用户已关注");
        }
        UserFollowList userFollowList=new UserFollowList();
        userFollowList.setId(SysUtil.UUID());
        userFollowList.setFollowUserId(followUserId);
        userFollowList.setUserId(SysUtil.getLoginUserId(request));
        userFollowList.setCreateTime(LocalDateTime.now());
        userFollowList.setGroupId(groupId);
        this.save(userFollowList);
        return ResponseInfo.success();
    }

    /**
     * 批量新增
     * @param followUserIds
     * @return
     */
    @Override
    public Map<String, Object> addAll(String followUserIds,String groupId) {
        String[] followUserId=followUserIds.split(",");
        for (int i = 0; i <followUserId.length ; i++) {
            QueryWrapper<UserFollowList> qw=new QueryWrapper<>();
            qw.eq("follow_user_id",followUserId[i]);
            qw.eq("user_id",SysUtil.getLoginUserId(request));
            qw.eq("group_id",groupId);
            if (this.count(qw)>0){
                continue;
            }
            UserFollowList userFollowList=new UserFollowList();
            userFollowList.setId(SysUtil.UUID());
            userFollowList.setFollowUserId(followUserId[i]);
            userFollowList.setUserId(SysUtil.getLoginUserId(request));
            userFollowList.setCreateTime(LocalDateTime.now());
            userFollowList.setGroupId(groupId);
            this.save(userFollowList);
        }
        return ResponseInfo.success();
    }

    /**
     * 根据关注人id删除关注
     * @param followUserId
     * @return
     */
    @Override
    public Map<String, Object> removeByFollowUserId(String followUserId,String groupId) {
        QueryWrapper<UserFollowList> qw=new QueryWrapper<>();
        qw.eq("follow_user_id",followUserId);
        qw.eq("user_id",SysUtil.getLoginUserId(request));
        if(!StringUtils.isEmpty(groupId)){
            qw.eq("group_id",groupId);
            System.out.println("******-----"+groupId);
        }
        this.remove(qw);
        return ResponseInfo.success();
    }

    /**
     * 根据关注人id批量删除关注
     * @param followUserIds
     * @return
     */
    @Override
    public Map<String, Object> removeAllByFollowUserId(String followUserIds,String groupId) {
        String[] followUserId=followUserIds.split(",");
//        for (int i = 0; i <followUserId.length ; i++) {
            QueryWrapper<UserFollowList> qw=new QueryWrapper<>();
            qw.in("follow_user_id",followUserId);
            qw.eq("user_id",SysUtil.getLoginUserId(request));
            qw.eq("group_id",groupId);
            this.remove(qw);
//        }
        return ResponseInfo.success();
    }

    /**
     * 删除关注
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> removeById(String id) {
        this.removeById(id);
        return ResponseInfo.success();
    }
    /**
     * 查询所有关注人信息
     * @return
     */
    @Override
    public Map<String, Object> getAll(String groupId) {
        QueryWrapper<UserFollowList> qw=new QueryWrapper<>();
        qw.eq("user_id",SysUtil.getLoginUserId(request));
        qw.eq("group_id",groupId);
        List<UserFollowList> lists=this.list(qw);
        return ResponseInfo.success(lists);
    }
}
